/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SelectClass.java
 *
 * Created on 27/10/2013, 14:07:11
 */
package br.unisinos.simcop.builder.gui;

import br.unisinos.simcop.builder.repository.KnowClasses;
import br.unisinos.simcop.core.ISimcopClass;
import br.unisinos.simcop.core.Parameter;
import br.unisinos.simcop.core.Parameters;
import br.unisinos.simcop.core.process.SimcopTask;
import br.unisinos.simcop.data.source.ISequenceSource;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author tiago
 */
public class SelectClass extends javax.swing.JDialog {

    public static final int SEQUENCE_SOURCE = 0;
    public static final int INPUT_FILTER = 1;
    public static final int INPUT_TRANSFORM = 2;
    public static final int SEQUENCE_SIMILARITY = 3;
    public static final int CONTEXT_SIMILARITY = 4;
    public static final int OUTPUT_FILTER = 5;
    public static final int OUTPUT_TRANSFORM = 6;
    public static final int ATTRIBUTE_SIMILARITY = 7;
    private int type;
    private KnowClasses knowClasses;
    private ISimcopClass selected;

    /**
     * Creates new form SelectClass
     */
    public SelectClass(java.awt.Frame parent, int type, KnowClasses knowClasses) {
        super(parent, true);
        this.type = type;
        this.knowClasses = knowClasses;
        initComponents();
        setLocationRelativeTo(null);
        load();
        edFor.setSelectedIndex(2);
    }

    private void load() {
        pnlFor.setVisible(false);
        switch (type) {
            case SEQUENCE_SOURCE:
                lbType.setText("Sequence Source");
                tabClass.setModel(new TabClassModel(knowClasses.getForSources()));
                break;
            case INPUT_FILTER:
                lbType.setText("Input Filter");
                tabClass.setModel(new TabClassModel(knowClasses.getForInputFilters()));
                break;
            case INPUT_TRANSFORM:
                lbType.setText("Input Transformation");
                tabClass.setModel(new TabClassModel(knowClasses.getForInputTransformations()));
                break;
            case SEQUENCE_SIMILARITY:
                lbType.setText("Sequence Similarity");
                tabClass.setModel(new TabClassModel(knowClasses.getForSequences()));
                break;
            case CONTEXT_SIMILARITY:
                lbType.setText("Context Similarity");
                tabClass.setModel(new TabClassModel(knowClasses.getForContexts()));
                break;
            case ATTRIBUTE_SIMILARITY:
                lbType.setText("Attribute Similarity");
                tabClass.setModel(new TabClassModel(knowClasses.getForAttributes()));
                pnlFor.setVisible(true);
                break;
            case OUTPUT_FILTER:
                lbType.setText("Output Filter");
                tabClass.setModel(new TabClassModel(knowClasses.getForOutputFilters()));
                break;
            case OUTPUT_TRANSFORM:
                lbType.setText("Output Transformation");
                tabClass.setModel(new TabClassModel(knowClasses.getForOutputTransformations()));
                break;

        }

        tabClass.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
        int colWidth = 150;
        tabClass.getColumnModel().getColumn(0).setMaxWidth(colWidth);
        tabClass.getColumnModel().getColumn(0).setWidth(colWidth);
        tabClass.getColumnModel().getColumn(0).setPreferredWidth(colWidth);
        tabClass.updateUI();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        lbType = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        pnlFor = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        edFor = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        btConfirm = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabClass = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Select SimcopClass for this task");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setPreferredSize(new java.awt.Dimension(503, 50));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel3.setPreferredSize(new java.awt.Dimension(300, 46));

        lbType.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        lbType.setText("jLabel1");

        jLabel1.setText("Task:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(lbType, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 46, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addGap(6, 6, 6)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(lbType))
                    .addContainerGap(25, Short.MAX_VALUE)))
        );

        jPanel1.add(jPanel3, java.awt.BorderLayout.WEST);

        jLabel2.setText("For:");

        edFor.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        edFor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "TD: for TimeDescription Similarity", "LD: for LocationDescription Similarity", "SD: for Situation[.source][.aux][.predicate] Similarity", "ED: for Extended Data Similarity" }));

        javax.swing.GroupLayout pnlForLayout = new javax.swing.GroupLayout(pnlFor);
        pnlFor.setLayout(pnlForLayout);
        pnlForLayout.setHorizontalGroup(
            pnlForLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlForLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(edFor, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );
        pnlForLayout.setVerticalGroup(
            pnlForLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlForLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlForLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(edFor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.add(pnlFor, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setPreferredSize(new java.awt.Dimension(503, 45));

        btConfirm.setText("Confirm");
        btConfirm.setEnabled(false);
        btConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConfirmActionPerformed(evt);
            }
        });
        jPanel2.add(btConfirm);

        jButton1.setText("Cancel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton1);

        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);

        tabClass.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tabClass.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabClassMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabClass);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public String getFor() {
        switch (edFor.getSelectedIndex()) {
            case 0: return SimcopTask.FOR_TIME;
            case 1: return SimcopTask.FOR_LOCATION;
            case 2: return SimcopTask.FOR_SITUATION_DEFAULT;
            case 3: return SimcopTask.FOR_EXTENDED;
        }
        return "";
    }
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        selected = null;
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void tabClassMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabClassMouseClicked
        btConfirm.setEnabled(tabClass.getSelectedRow() >= 0);
    }//GEN-LAST:event_tabClassMouseClicked

    private void btConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConfirmActionPerformed
        int row = tabClass.getSelectedRow();
        if (row >= 0) {
            TabClassModel model = (TabClassModel) tabClass.getModel();
            if (model.available != null && row < model.available.size()) {
                selected = model.available.get(row);
                dispose();
            }
        }
    }//GEN-LAST:event_btConfirmActionPerformed

    public ISimcopClass getSelected() {
        return selected;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btConfirm;
    private javax.swing.JComboBox edFor;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbType;
    private javax.swing.JPanel pnlFor;
    private javax.swing.JTable tabClass;
    // End of variables declaration//GEN-END:variables

    class TabClassModel extends DefaultTableModel {

        private String[] columns = new String[]{"Class Name", "Default Parameters"};
        private List<ISimcopClass> available;

        public TabClassModel(List available) {
            this.available = available != null ? available : new ArrayList<ISimcopClass>();
        }

        @Override
        public String getColumnName(int column) {
            if (columns != null) {
                switch (column) {
                    case 0:
                        return columns[0];
                    case 1:
                        return columns[1];
                    default:
                        return "";
                }
            } else {
                return "";
            }
        }

        @Override
        public int getColumnCount() {
            return columns != null ? columns.length : 2;
        }

        @Override
        public int getRowCount() {
            return available != null ? available.size() : 1;
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }

        @Override
        public Object getValueAt(int row, int column) {
            ISimcopClass sc = available.get(row);
            switch (column) {
                case 0:
                    return sc.getClass().getSimpleName();
                case 1:
                    StringBuilder sb = new StringBuilder();
                    Parameters pars = sc.getDefaultParameters();
                    if (pars != null && !pars.isEmpty()) {
                        sb.append("{");
                        for (String parName : pars.keySet()) {
                            Parameter par = pars.get(parName);
                            if (par != null) {
                                sb.append(parName);
                                sb.append("=\"");
                                sb.append(par.getValue());
                                sb.append("\"; ");
                            }
                        }
                        sb.append("}");
                    } else {
                        sb.append(" - No Parameters -");
                    }
                    return sb.toString();
            }
            return "";
        }
    }
}
